﻿#pragma execution_character_set("utf-8")

#include "frmFramelessWidget.h"
#include "ui_frmFramelessWidget.h"

#include "qPushbutton.h"
#include "qCheckBox.h"
#include "qLabel.h"
#include "qDebug.h"
#include "FramelessWidget2.h"

#ifndef Q_CC_MSVC
#include "framelessform/Dialog.h"
#include "framelessform/Widget.h"
#include "framelessform/MainWindow.h"
#else
#include "framelesscore/FramelessDialog.h"
#include "framelesscore/FramelessWidget.h"
#include "framelesscore/FramelessMainWindow.h"
#endif

frmFramelessWidget::frmFramelessWidget(QWidget *parent) : QWidget(parent), ui(new Ui::frmFramelessWidget)
{
    ui->setupUi(this);
    this->initForm();
}

frmFramelessWidget::~frmFramelessWidget()
{
    delete ui;
}

void frmFramelessWidget::initForm()
{
    widget = Q_NULLPTR;
    frameless = Q_NULLPTR;

    framelessDialog = Q_NULLPTR;
    framelessWidget = Q_NULLPTR;
    framelessWindow = Q_NULLPTR;

    connect(ui->btnDialog, SIGNAL(clicked(bool)), this, SLOT(buttonClicked()));
    connect(ui->btnWidget, SIGNAL(clicked(bool)), this, SLOT(buttonClicked()));
    connect(ui->btnMainWindow, SIGNAL(clicked(bool)), this, SLOT(buttonClicked()));
}

void frmFramelessWidget::initWidget(QWidget *w)
{
    //设置无边框属性
    w->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
    //w->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
    w->setWindowTitle("自由拉伸无边框窗体");
    w->setMinimumSize(200, 120);
    w->resize(480, 320);

    //设置下背景颜色区别看
    QPalette palette = w->palette();
    palette.setBrush(QPalette::Window, QColor(162, 121, 197));
    w->setPalette(palette);

    QPushButton *btn = new QPushButton(w);
    btn->setText("关闭");
    btn->setGeometry(10, 10, 130, 25);
    connect(btn, SIGNAL(clicked(bool)), w, SLOT(close()));

    QCheckBox *cboxMove = new QCheckBox(w);
    cboxMove->setText("可移动");
    cboxMove->setChecked(true);
    cboxMove->setGeometry(10, 40, 70, 25);
    connect(cboxMove, SIGNAL(stateChanged(int)), this, SLOT(stateChanged1(int)));

    QCheckBox *cboxResize = new QCheckBox(w);
    cboxResize->setText("可拉伸");
    cboxResize->setChecked(true);
    cboxResize->setGeometry(80, 40, 70, 25);
    connect(cboxResize, SIGNAL(stateChanged(int)), this, SLOT(stateChanged2(int)));
}

template <typename TWidget>
void frmFramelessWidget::initFramelessWidget(TWidget *widget)
{
    QWidget *widgetBase = static_cast<QWidget *>(widget);
    if (widgetBase == Q_NULLPTR)
        return;

    //设置无边框属性
    widgetBase->setWindowFlags(Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
    //widgetBase->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
    widgetBase->setWindowTitle("自由拉伸无边框窗体");
    widgetBase->setMinimumSize(200, 120);
    widgetBase->resize(480, 320);

    //设置下背景颜色区别看
    QPalette palette = widgetBase->palette();
    palette.setBrush(QPalette::Window, QColor(162, 121, 197));
    widgetBase->setPalette(palette);

    QLabel *titleBar = new QLabel(widgetBase);
    titleBar->setText("标  题  栏");
    titleBar->setGeometry(0, 0, widgetBase->width(), 36);
    titleBar->setStyleSheet("color: white; font-weight: bold; background-color: red;");
    titleBar->setAlignment(Qt::AlignCenter);
    widget->setTitleBar(titleBar);

    QPushButton *btn = new QPushButton(widgetBase);
    btn->setText("关闭");
    btn->setGeometry(10, 70, 130, 25);
    connect(btn, SIGNAL(clicked(bool)), widgetBase, SLOT(close()));

    QCheckBox *cboxMove = new QCheckBox(widgetBase);
    cboxMove->setText("可移动");
    cboxMove->setChecked(true);
    cboxMove->setGeometry(10, 40, 70, 25);
    connect(cboxMove, SIGNAL(stateChanged(int)), this, SLOT(stateChanged1(int)));

    QCheckBox *cboxResize = new QCheckBox(widgetBase);
    cboxResize->setText("可拉伸");
    cboxResize->setChecked(true);
    cboxResize->setGeometry(80, 40, 70, 25);
    connect(cboxResize, SIGNAL(stateChanged(int)), this, SLOT(stateChanged2(int)));

    titleBar->lower();
    //titleBar->hide();
}

void frmFramelessWidget::on_pushButton_clicked()
{
    if (widget == 0) {
        widget = new QWidget;
        this->initWidget(widget);
        frameless = new FramelessWidget2(widget);
        frameless->setWidget(widget);
    }

    widget->show();
}

void frmFramelessWidget::stateChanged1(int arg1)
{
    if (frameless != Q_NULLPTR) {
        frameless->setMoveEnable(arg1 != 0);
    }

    if (framelessDialog != Q_NULLPTR) {
        framelessDialog->setMoveEnable(arg1 != 0);
        framelessDialog->update();
    }

    if (framelessWidget != Q_NULLPTR) {
        framelessWidget->setMoveEnable(arg1 != 0);
        framelessWidget->update();
    }

    if (framelessWindow != Q_NULLPTR) {
        framelessWindow->setMoveEnable(arg1 != 0);
        framelessWindow->update();
    }
}

void frmFramelessWidget::stateChanged2(int arg1)
{
    if (frameless != Q_NULLPTR) {
        frameless->setResizeEnable(arg1 != 0);
    }

    if (framelessDialog != Q_NULLPTR) {
        framelessDialog->setResizeEnable(arg1 != 0);
        framelessDialog->update();
    }

    if (framelessWidget != Q_NULLPTR) {
        framelessWidget->setResizeEnable(arg1 != 0);
        framelessWidget->update();
    }

    if (framelessWindow != Q_NULLPTR) {
        framelessWindow->setResizeEnable(arg1 != 0);
        framelessWindow->update();
    }
}

void frmFramelessWidget::buttonClicked()
{
#ifndef Q_CC_MSVC
    QString objName = sender()->objectName();
    if (objName == "btnDialog") {
        Dialog dialog;
        dialog.exec();
    } else if (objName == "btnWidget") {
        Widget *widget = new Widget;
        widget->show();
    } else if (objName == "btnMainWindow") {
        MainWindow *window = new MainWindow;
        window->show();
    }
#else
    QString objName = sender()->objectName();
    qDebug() << "objName:" << objName;
    if (objName == "btnDialog") {
        framelessDialog = new FramelessDialog;
        this->initFramelessWidget<FramelessDialog>(framelessDialog);
        framelessDialog->setWindowTitle("自由拉伸无边框窗体");
        framelessDialog->setMinimumSize(200, 120);
        framelessDialog->resize(480, 320);
        framelessDialog->exec();
    } else if (objName == "btnWidget") {
        framelessWidget = new FramelessWidget;
        this->initFramelessWidget<FramelessWidget>(framelessWidget);
        framelessWidget->setWindowTitle("自由拉伸无边框窗体");
        framelessWidget->setMinimumSize(200, 120);
        framelessWidget->resize(480, 320);
        framelessWidget->show();
    } else if (objName == "btnMainWindow") {
        framelessWindow = new FramelessMainWindow;
        this->initFramelessWidget<FramelessMainWindow>(framelessWindow);
        framelessWindow->setWindowTitle("自由拉伸无边框窗体");
        framelessWindow->setMinimumSize(200, 120);
        framelessWindow->resize(480, 320);
        framelessWindow->show();
    }
#endif
}
